package com.tosit.bmx.controller.user;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;


import com.tosit.bmx.domian.ErrorTopic;
import com.tosit.bmx.domian.TempSubject;
import com.tosit.bmx.domian.User;
import com.tosit.bmx.service.user.ErrorService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 错题管理类
 *
 * @author tsl
 * @create 2018-04-27 15:28
 **/
@Controller
@RequestMapping()
public class ErrorController {
    @Autowired
    ErrorService errorService;

    /**
     * 根据用户id获取所有错题
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public void gainErrorAll(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        List<TempSubject> result = errorService.gainErrorAll(user.getId());
        if (result != null) {
            JSONModel.put("data", result);
            JSONModel.put("user", user);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 根据用户id获取所有错题数量
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/error/count", method = RequestMethod.GET)
    public void gainErrorCount(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer result = errorService.gainErrorCount(user.getId());
        if (result != null) {
            JSONModel.put("count", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 添加错题
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/error", method = RequestMethod.POST)
    public void addError(ErrorTopic error, HttpServletResponse response) {

        Integer result = null;
        ErrorTopic isExist = errorService.isExist(error);
        if (isExist == null) {
            result = errorService.addError(error);
        }
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 根据用户id删除错题
     *
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/error", method = RequestMethod.DELETE)
    public void removeError(HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer result = errorService.removeError(user.getId());
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }
}
